(function() {
  var submit_search_form;

  submit_search_form = function(page_no) {
    var form, page_rows;
    form = $('form#search_form');
    if (page_no) {
      form.form('set value', 'page_no', page_no);
    }
    page_rows = Cookies.get('table.page_rows');
    if (!page_rows) {
      page_rows = 10;
      Cookies.set('table.page_rows', page_rows, {
        expires: 365,
        path: "/"
      });
    }
    form.form('set value', 'page_rows', page_rows);
    return form.submit();
  };

  $(function() {
    var page_rows;
    $('.ui.search input[name=keyword]').change(function() {
      return $('form.ui.form#search_form').form('set value', 'page_no', 1);
    });
    $('.ui.search input[name=keyword]').keypress(function(evt) {
      if (evt.which === 13) {
        evt.preventDefault();
        return submit_search_form(1);
      }
    });
    $('.ui.search .icon.search').click(function() {
      $(this).addClass('loading');
      return submit_search_form(1);
    });
    $('form#search_form .dropdown').dropdown({
      onChange: function(value, text, $choice) {
        return submit_search_form(1);
      }
    });
    $('form#search_form .ui.calendar .icon#date_clear').click(function() {
      return $('form#search_form').form('set values', {
        start_date: '',
        end_date: ''
      });
    });

    /*
     * 设置表格底部的翻页控件跨越整行
    n_cols = $("table#table > thead").find("> tr:first > th").length
    $('tfoot > tr > th').attr('colspan', n_cols)
     */
    page_rows = Cookies.get('table.page_rows');
    if (page_rows) {
      $('.ui.pagination .dropdown#page_rows').dropdown('set value', page_rows);
    }
    $('.ui.pagination .dropdown#page_rows').dropdown({
      onChange: function(value, text, $selected) {
        $(this).addClass('loading');
        Cookies.set('table.page_rows', value, {
          expires: 365,
          path: "/"
        });
        return submit_search_form(1);
      }
    });
    $('.ui.pagination a.item').click(function() {
      var page_no;
      page_no = $(this).data('page-no');
      return submit_search_form(page_no);
    });
    $('.ui.pagination input').keypress(function(evt) {
      var page_no;
      if (evt.which === 13) {
        page_no = parseInt($(this).val());
        if (isNaN(page_no)) {
          alert('您输入的页码 "' + $(this).val() + '" 格式有误，' + '页码必须是介于 1 和 最大页码数之间的正整数。');
          return;
        }
        if (page_no < 1 || page_no > $('#page_num').data('page-num')) {
          return;
        }
        if (page_no === $('form.form#search_form input[name="page_no"]').val()) {
          return;
        }
        return submit_search_form(page_no);
      }
    });
    $('table.table#table tbody tr').click(function() {
      $(this).toggleClass('active');
      if ($(this).hasClass('active')) {
        if (Cookies.get('settings.table_active_unique') === 'on') {
          return $(this).siblings('tr').removeClass('active');
        }
      }
    });
    return $('i.detail.expand').click(function() {
      var detail;
      detail = $(this).parents('tr').next('tr.detail');
      if (detail.is(':visible')) {
        detail.hide('fast');
        return $(this).removeClass('down').addClass('right');
      } else {
        $('i.down').removeClass('down').addClass('right');
        $('table tr.detail').hide();
        detail.show('fast');
        return $(this).removeClass('right').addClass('down');
      }
    });
  });

}).call(this);
